package com.dim.list_orders;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class main extends Activity {

	boolean customTitleSupported;
	private class_function funct = new class_function();
	SharedPreferences prefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(com.dim.list_orders.R.layout.main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				com.dim.list_orders.R.layout.title);
		TextView title = (TextView) findViewById(com.dim.list_orders.R.id.title);
		title.setText(com.dim.list_orders.R.string.l_main);
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		font_size();
	}

	void font_size() {
		TextView textView1 = (TextView) findViewById(com.dim.list_orders.R.id.textView1);
		TextView textView2 = (TextView) findViewById(com.dim.list_orders.R.id.textView2);
		TextView textView3 = (TextView) findViewById(com.dim.list_orders.R.id.textView3);
		TextView textView4 = (TextView) findViewById(com.dim.list_orders.R.id.textView4);
		String str = prefs.getString("font_size", "2");
		if (str.equals("1")) {
			textView1.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_small));
			textView2.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_small));
			textView3.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_small));
			textView4.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_small));
		} else if (str.equals("3")) {
			textView1.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_large));
			textView2.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_large));
			textView3.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_large));
			textView4.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_large));
		} else {
			textView1.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_medium));
			textView2.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_medium));
			textView3.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_medium));
			textView4.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_medium));
		}
	}

	public void show_orders(View v) {
		Intent i = new Intent(this, orders.class);
		startActivity(i);
	}

	public void show_users(View v) {
		Intent i = new Intent(this, users.class);
		startActivity(i);
	}

	public void show_warehouse(View v) {
		Intent i = new Intent(this, warehous.class);
		startActivity(i);
	}

	public void show_items(View v) {
		Intent i = new Intent(this, items.class);
		startActivity(i);
	}

	void backup_db() {
		if (prefs.getBoolean("c_db_backup", false)) {
			funct.file_backup(this,
					getString(com.dim.list_orders.R.string.db_name));
		}

		if (prefs.getBoolean("c_db_backups_all", false)) {
			funct.file_delete_old(new File(Environment
					.getExternalStorageDirectory()
					+ "/Backup/"
					+ getBaseContext().getPackageName() + "/"));
		}
	}

	public void onBackPressed() {
		backup_db();
		System.exit(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, 2, Menu.NONE,
				com.dim.list_orders.R.string.m_restory).setIcon(
				android.R.drawable.ic_menu_recent_history);
		menu.add(Menu.NONE, 0, Menu.NONE, com.dim.list_orders.R.string.m_exit)
				.setIcon(android.R.drawable.ic_lock_power_off);
		menu.add(Menu.NONE, 4, Menu.NONE,
				com.dim.list_orders.R.string.m_preference).setIcon(
				android.R.drawable.ic_menu_preferences);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			System.exit(0);
			break;

		case 2:
			Intent a = new Intent(this, backup_db.class);
			startActivity(a);
			break;
		case 4:
			Intent i = new Intent(this, preferences.class);
			startActivity(i);
			break;
		default:
			break;
		}

		return false;
	}

}